<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="比赛名称">
          &lt;!&ndash;<a-input placeholder="请输入比赛名称" v-decorator="['nameId', {}]" />&ndash;&gt;
          <j-dict-select-tag v-decorator="['nameId', validatorRules.nameId]"
                             :triggerChange="true"
                             placeholder="请选择比赛"
                             dictCode='g_name,match_name,id'/>
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="赛事名称">
          <a-input placeholder="请输入赛事名称" v-decorator="['name', validatorRules.name]" :max-length="15"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="大区">
          <j-dict-select-tag v-decorator="['region', validatorRules.region]"
                             :triggerChange="true"
                             placeholder="请选择比赛"
                             dictCode='region'/>
          <!--<a-input placeholder="请输入大区" v-decorator="['region', {}]" />-->
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="已报名人数">
          <a-input placeholder="请输入已报名人数" v-decorator="['signPerson', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="报名人数下限">
          <a-input-number placeholder="请输入报名人数下限" v-decorator="['minPerson', validatorRules.minPerson]" :min="0"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="报名人数上限">
          <a-input-number placeholder="请输入报名人数上限" v-decorator="['maxPerson', validatorRules.maxPerson]" :min="0"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="最低段位">
          <a-select placeholder="请选择最低段位"
                    v-decorator="['lowestRank',validatorRules.lowestRank]">
            <a-select-option v-for="d in rankList" :key="d.id" :value="d.id">{{ d.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="最高段位">
          <a-select placeholder="请选择最高段位"
                    v-decorator="['highestRank',validatorRules.highestRank]">
            <a-select-option v-for="d in rankList" :key="d.id" :value="d.id">{{ d.name }}</a-select-option>
          </a-select>
          <!--<a-input placeholder="请输入最高段位" v-decorator="['highestRank', {}]" />-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="报名开始时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'signUpTime', validatorRules.signUpTime]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="报名截至时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'signEndTime',validatorRules.signEndTime]" />
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="赛事进程">
          <a-input placeholder="请输入0:进行中,1:已完成,2:取消,3:未开始" v-decorator="['start', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="比赛开始时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'startTime',validatorRules.startTime]" />
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="结束时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'endTime', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :required="true"
          label="赏金计划">
          <a-row :gutter="24">
            <a-col :span="24">
              <a-form-item style="min-height: 300px">
                <j-editor v-model="rewardScheme" ></j-editor>
              </a-form-item>
            </a-col>
          </a-row>
          <!--<a-input placeholder="请输入赏金计划" v-decorator="['rewardScheme', {}]" />-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :required="true"
          label="比赛规则">
          <a-row :gutter="24">
            <a-col :span="24">
              <a-form-item style="min-height: 300px">
                <j-editor v-model="rule" ></j-editor>
              </a-form-item>
            </a-col>
          </a-row>
          <!--<a-input placeholder="请输入规则" v-decorator="['rule', {}]" />-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="进入房间链接">
          <a-input placeholder="请输入进入房间链接" v-decorator="['inHomeUrl', {}]" :max-length="500"/>
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="0:正常,1:删除">
          <a-input placeholder="请输入0:正常,1:删除" v-decorator="['delflag', {}]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-input placeholder="请输入备注" v-decorator="['remark', {}]" :max-length="30"/>
        </a-form-item>
        <!--<a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark1">
          <a-input placeholder="请输入remark1" v-decorator="['remark1', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark2">
          <a-input placeholder="请输入remark2" v-decorator="['remark2', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark3">
          <a-input placeholder="请输入remark3" v-decorator="['remark3', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark4">
          <a-input placeholder="请输入remark4" v-decorator="['remark4', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark5">
          <a-input placeholder="请输入remark5" v-decorator="['remark5', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark6">
          <a-input placeholder="请输入remark6" v-decorator="['remark6', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark7">
          <a-input placeholder="请输入remark7" v-decorator="['remark7', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark8">
          <a-input placeholder="请输入remark8" v-decorator="['remark8', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark9">
          <a-input placeholder="请输入remark9" v-decorator="['remark9', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="remark10">
          <a-input placeholder="请输入remark10" v-decorator="['remark10', {}]" />
        </a-form-item>-->

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import { getAction } from '@/api/manage'
  import JEditor from '@/components/jeecg/JEditor'


  export default {
    name: "MatchModal",
    components: {
      JEditor
    },
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          name:{rules: [{ required: true, message: '请输入比赛名称!' }]},
          region:{rules: [{ required: true, message: '请输入比赛名称!' }]},
          minPerson:{rules: [{ required: true, message: '请输入报名人数下限!' }]},
          maxPerson:{rules: [{ required: true, message: '请输入报名人数上限!' }]},
          lowestRank:{rules: [{ required: true, message: '请选择最低段位!' }]},
          highestRank:{rules: [{ required: true, message: '请选择最高段位!' }]},
          signUpTime:{rules: [{ required: true, message: '请选择报名开始时间!' }]},
          signEndTime:{rules: [{ required: true, message: '请选择报名截至时间!' }]},
          startTime:{rules: [{ required: true, message: '请选择比赛开始时间!' }]},
        },
        url: {
          add: "/match/match/add",
          edit: "/match/match/edit",
        },
        rankList:[],
        rewardScheme:'',
        rule:'',

      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.nameId = this.$route.query.nameId;
        record.nameId = this.nameId;
        record.start = "3";
        record.delflag = "0";
        this.rewardScheme = record.rewardScheme;
        this.rule = record.rule;

        if(record.nameId != null){
          this.loadRankList(record.nameId)
        }
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'nameId','name','region','signPerson','minPerson','maxPerson','lowestRank','highestRank','start','rewardScheme','rule','inHomeUrl','result','delflag','remark','remark1','remark2','remark3','remark4','remark5','remark6','remark7','remark8','remark9','remark10'))
		  //时间格式化
          this.form.setFieldsValue({signUpTime:this.model.signUpTime?moment(this.model.signUpTime):null})
          this.form.setFieldsValue({signEndTime:this.model.signEndTime?moment(this.model.signEndTime):null})
          this.form.setFieldsValue({startTime:this.model.startTime?moment(this.model.startTime):null})
        });

      },

      loadRankList(value) {
        this.rankList = []
        getAction('/match/match/getRankByNameId', { 'nameId': value }).then((res) => {
          if (res.success) {
            this.rankList = res.result
          } else {
            console.log(res.message)

          }
        })
      },

      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {

        const that = this;
        if (this.rewardScheme == 'undefined' || this.rewardScheme == '') {
          this.$message.warning('请输入赏金计划！')
          return false
        };
        if (this.rule == 'undefined' || this.rule == '') {
          this.$message.warning('请输入比赛规则！')
          return false
        }
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData.rewardScheme = this.rewardScheme
            formData.rule = this.rule
            //时间格式化
            /*formData.signUpTime = formData.signUpTime?formData.signUpTime.format('YYYY-MM-DD HH:mm:ss'):null;*/
            formData.signEndTime = formData.signEndTime?formData.signEndTime.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.startTime = formData.startTime?formData.startTime.format('YYYY-MM-DD HH:mm:ss'):null;

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>